#include "stdafx.h"

#include <stdio.h>
#include <windows.h>

#include "Handle.h"

using namespace WCMM;

CHandle::CHandle() throw() :
m_h(NULL)
{
}

CHandle::CHandle(_Inout_ CHandle& h) throw() :
m_h(NULL)
{
	Attach( h.Detach() );
}

CHandle::CHandle(_In_ HANDLE h) throw() :
m_h(h)
{
}

CHandle::~CHandle() throw()
{
	if( m_h != NULL ){
		Close();
	}
}

CHandle& CHandle::operator=(_Inout_ CHandle& h) throw()
{
	if( this != &h ){
		if(m_h != NULL){
			Close();
		}
		Attach(h.Detach());
	}

	return( *this );
}

CHandle::operator HANDLE() const throw()
{
	return m_h;
}

void CHandle::Attach(_In_ HANDLE h) throw()
{
	ASSERTE(m_h == NULL);
	m_h = h;
}

HANDLE CHandle::Detach() throw()
{
	HANDLE h;

	h = m_h; 
	m_h = NULL;

	return h;
}

void CHandle::Close() throw()
{
	if( m_h != NULL ){
		::CloseHandle( m_h );
		m_h = NULL;
	}
}